//
//  ListAllPhotoAlbumsView.m
//  BabyHelper
//
//  Created by  thomas on 11-8-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListAllPhotoAlbumsView.h"


@implementation ListAllPhotoAlbumsView
@synthesize photoAlbumList;
@synthesize lastIndexPath;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = NSLocalizedString(@"All Albums",@"");
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
	photoAlbumList = [[NSMutableArray alloc]init];
	
	dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:DATE_TIME_FORMAT];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	Dao *dao = [Dao getInstance];
	[photoAlbumList removeAllObjects];
	[dao QueryPhotoCategoryEntryWith:@"ORDER BY ID ASC" records:photoAlbumList];
	[self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [photoAlbumList count] + 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	NSLog(@"row is %d",indexPath.row);
	if(indexPath.row == 0)
	{
		cell.textLabel.text       =  NSLocalizedString(@"Other photo album", @"");
		cell.detailTextLabel.text = [dateFormatter stringFromDate:[NSDate date]];				
	}
	else {
		int realRow = indexPath.row -1;
		PhotoAlbumEntry *entry = [photoAlbumList objectAtIndex:realRow];
		cell.textLabel.text       = entry.name;
		cell.detailTextLabel.text = [dateFormatter stringFromDate:entry.timestamp];		
	}

	NSInteger row = [indexPath row];
	NSInteger oldRow = [lastIndexPath row];
	
	cell.accessoryType = (row == oldRow && lastIndexPath !=nil )? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSInteger newrow = [indexPath row];
	NSInteger oldRow = (lastIndexPath!=nil)? [lastIndexPath row]:-1;
	
	if(newrow != oldRow)
	{
		UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
		newCell.accessoryType = UITableViewCellAccessoryCheckmark;

		UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
		oldCell.accessoryType = UITableViewCellAccessoryNone;
		lastIndexPath = indexPath;
		
		NSUserDefaults *prefs   = [NSUserDefaults standardUserDefaults];
		if ([indexPath row] == 0 ) {
			[prefs setDouble:0 forKey:@"CurrentPhotoAlbumIndex"];
		}
		else {
			int realRow = indexPath.row - 1;
			PhotoAlbumEntry *entry = [self.photoAlbumList objectAtIndex:realRow];
			[prefs setDouble:entry.index forKey:@"CurrentPhotoAlbumIndex"];
		}
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

